Add #[thiserror(crate = "...")] attribute for re-export scenarios#450
Add #[thiserror(crate = "...")] attribute for re-export scenarios#450suchyj-btlnet wants to merge 2 commits intodtolnay:masterfrom
Conversation
Allow users to specify the path to the thiserror crate using `#[thiserror(crate = "...")]` on the container. All internal references to the crate now use the specified path, defaulting to `::thiserror` if not provided.
There was a problem hiding this comment.
As discussed in the issue, I would prefer not to provide this workaround. Please follow the suggested implementation from #167 (comment).
Sorry @dtolnay, could you please point me to the suggested workaround? If you ment #167 (comment) from @alexkirsz, that doesn't work in my repro. If you ment your comment in rust-lang/rust#54363 (comment) that is not an implemented feature in cargo yet. 😑 Thanks Edit: Sorry for my lack of knowledge, I'm in rust ecosystem only for 1.5years, I have limited understanding of macros and I don't understand this problem fully. |



When a crate re-exports
thiserrorand exposes a macro that generates#[derive(Error)]types, the consuming crate must still listthiserroras a direct dependency — because the derive unconditionally emits::thiserror::absolute paths into the generated code. This makesthiserrora leaky dependency that cannot be fully encapsulated behind a re-export.This PR adds a
#[thiserror(crate = "...")]container attribute, following the same pattern as#[serde(crate = "...")](serde-rs/serde#1499). When present, all generated paths use the specified crate root instead of::thiserror.Repro: https://github.com/suchyj-btlnet/thiserror-reexport-repro —
consumerfails to build because it has no directthiserrordependency.To validate the fix, clone this PR's branch next to the repro repo, then:
[patch.crates-io]in the repro's rootCargo.toml#[thiserror(crate = "macro_lib::thiserror")]inmacro-lib/src/lib.rscargo build -p consumernow succeedsTo be honest, @dtolnay doesn't prefer this from here #167
however @dtolnay also approved and merged this exact fix in
serde(above).So I hope this PR will get merged to get this functionality also in
thiserrorCheers, Jacob